/**
 * A configuration is something that individual users own and is attached to a page.
 * Because the keystroke and mouse events are the only inputs for the program, we process
 * these events
 * 
 */
import java.util.ArrayList;
import processing.core.PApplet;

public class Configuration {
  
  private Page page;
  private String ip;
  private int[]  mode, drawWidth, drawColor, drawType, textSize, textColor;
  private int state;
  
  private PApplet parent;
  
  public Configuration(PApplet p, String _ip, int dwidth, int dcolor, int tsize, int tcolor){
    parent = p;
    state = 0;
    ip = _ip;
    mode = new int[1000];
    drawWidth = new int[1000];
    drawColor = new int[1000];
    drawType= new int[1000];
    textSize = new int[1000];
    textColor = new int[1000];
    mode[0] = 0; //starts out in drawing mode
    drawWidth[0] = dwidth;
    drawType[0] = 0;
    drawColor[0] = dcolor; // 0 - 256
    textSize[0] = tsize;
    textColor[0] = tcolor; // 0 - 256
  }
  
  public Configuration(PApplet p, String _ip, int _mode, int dwidth, int _type, int dcolor, int tsize, int tcolor){
    parent = p;
    state = 0;
    ip = _ip;
    mode = new int[1000];
    drawWidth = new int[1000];
    drawColor = new int[1000];
    drawType= new int[1000];
    textSize = new int[1000];
    textColor = new int[1000];
    mode[0] = _mode; //starts out in drawing mode
    drawWidth[0] = dwidth;
    drawType[0] = _type;
    drawColor[0] = dcolor; // 0 - 256
    textSize[0] = tsize;
    textColor[0] = tcolor; // 0 - 256
  }
  
  public Configuration(PApplet p, String encodedConfiguration) {
    parent = p;
    state = 0;
    mode = new int[1000];
    drawWidth = new int[1000];
    drawColor = new int[1000];
    drawType= new int[1000];
    textSize = new int[1000];
    textColor = new int[1000];
    
    String[] basicElements = parent.split(encodedConfiguration, '/');
    if(!basicElements[1].equalsIgnoreCase("configuration")) {
      parent.println("There is a problem decoding a configuration object: " + encodedConfiguration + " , " + basicElements[1]);
      return;
    } else {
      ip = new String(basicElements[2]);
      mode[0] = new Integer(basicElements[3]).intValue();
      drawWidth[0] = new Integer(basicElements[4]).intValue();
      drawType[0] = new Integer(basicElements[5]).intValue();
      drawColor[0] = new Integer(basicElements[6]).intValue();
      textSize[0] = new Integer(basicElements[7]).intValue();
      textColor[0] = new Integer(basicElements[8]).intValue();
    }
  }
  
  //copies over old state to new one for updating with changes
  private void updateState() {
    int tmp = state;
    state++;
    mode[state] = mode[tmp];
    drawWidth[state] = drawWidth[tmp];
    drawColor[state] = drawColor[tmp];
    drawType[state] = drawType[tmp];
    textSize[state] = textSize[tmp];
    textColor[state] = textColor[tmp];
  }
  
  private void change(int i) {
    switch(i) {
      case(0):
        //update everything
        ((thetablet)parent).updateConfigList();
        break;
      case(1):
        //update mode
        ((thetablet)parent).updateConfigList("mode");
        break;
      default:
        break;
    }
    
  }
  
  public void updateState(int _mode, int _dwidth, int _dtip, int _dcol, int _tsize, int _tcol) {
    setMode(_mode);
    setDrawWidth(_dwidth);
    setDrawType(_dtip);
    setDrawColor(_dcol);
    setTextSize(_tsize);
    setTextColor(_tcol);
  }
  
  public void setMode(int _mode) {
    if (mode[state] != _mode) {
      updateState();
      mode[state] = _mode;
      change(00);
    }
  }
  
  public void setDrawWidth(int _width) {
    if (drawWidth[state] != _width) {
      updateState();
      drawWidth[state] = _width;
      change(11);
    }
  }
  
  public void setDrawType(int _type) {
    if (drawType[state] != _type) {
      updateState();
      drawType[state] = _type;
      change(12);
    }
  }
  
  public void setDrawColor(int _hue) {
    if (drawColor[state] != _hue) {
      updateState();
      drawColor[state] = _hue;
      change(13);
    }
  }
  
  public void setTextSize(int _size) {
    if (textSize[state] != _size) {
      updateState();
      textSize[state] = _size;
      change(20);
    }
  }
  
  public void setTextColor(int _color) {
    if (textColor[state] != _color) {
      updateState();
      textColor[state] = _color;
      change(21);
    }
  }
  
  public void setPage(Page _page) {
    page = _page;
  }
  
  public void setIP(String _ip) {
    ip = _ip;
  }
  
  public String getIP() {
    return ip;
  }
  
  public int getMode() {
    return mode[state];
  }
  
  public int getDrawWidth() {
    return drawWidth[state];
  }
  
  public int getDrawType() {
    return drawType[state];
  }
  
  public int getDrawColor() {
    return drawColor[state];
  }
  
  public int getTextSize() {
    return textSize[state];
  }
  
  public int getTextColor() {
    return textColor[state];
  }
  
  public String toString() {
    String tmp = null;
    tmp = "/configuration/" + getIP() + "/" + getMode() + "/" + getDrawWidth() + "/" + getDrawType() + "/" + getDrawColor() + "/" + getTextSize() + "/" + getTextColor();
    return tmp;
  }
  
}
